package com.example.window.stateless

import java.io.PrintWriter
import java.net.{ServerSocket, Socket}

// 每一秒钟产生一个数字
object SocketLikeNCForNum {
  def main(args: Array[String]): Unit = {
    val port = 8888
    val server = new ServerSocket(port)
    var i: Long = 0
    while (true) {
      val socket: Socket = server.accept()
      println("成功连接到本地主机：" + socket.getInetAddress)
      new ServerThread(socket).start()
      Thread.sleep(100)
    }
  }
}

class ServerThread(sock: Socket) extends Thread {


  override def run(): Unit = {
    val out = new PrintWriter(sock.getOutputStream)
    val random = scala.util.Random
    var i: Int = 1
    while (true) {
      println(i)
      out.println(i)
      out.flush()
      i += 1
//      if (i == 11) {
//        i = 1
//      }
      Thread.sleep(1000)
    }
  }
}
